const Router = require('koa-router')
const SystemController = require('../app/controllers/rbac/SystemController')
const RoleController = require('../app/controllers/rbac/RoleController')
const PermissionController = require('../app/controllers/rbac/PermissionController')
const AdminController = require('../app/controllers/rbac/AdminController')
const NodeController = require('../app/controllers/rbac/NodeController')
const MenuController = require('../app/controllers/MenuController')
const AuthController = require('../app/controllers/auth/AuthController')

class Rbac {
    constructor() {
        this.$rbac = new Router({prefix: '/rbac'})
        this.init()
        this.routes()
        return this.$rbac
    }

    /**
     * 初始化路由 加载资源路由
     * 
     * @memberof Api
     */
    async init () {
        await this.resource('system', SystemController)        
        await this.resource('role', RoleController)        
        await this.resource('permission', PermissionController)        
        await this.resource('admin', AdminController)        
        await this.resource('node', NodeController)        
    }
    // 定义普通路由
    async routes() {
        this.$rbac.get('/system/roles/:system_key', SystemController.roles)
        this.$rbac.get('/system/permissions/:system_key', SystemController.permissions)
        this.$rbac.post('/system/:system_key/role/:role_id', SystemController.rolePermissions)
        this.$rbac.post('/menu/get', MenuController.get)
        this.$rbac.post('/signin', AuthController.signin)
    }

    /**
     * 生成资源路由
     * 
     * @param {any} name 
     * @param {any} controller 
     * @memberof Api
     */
    async resource (name, controller) {
        this.$rbac.get(`/${name}`, controller.index)
        this.$rbac.get(`/${name}/create`,  controller.create)
        this.$rbac.post(`/${name}`,  controller.store)
        this.$rbac.get(`/${name}/:id`,  controller.show)
        this.$rbac.get(`/${name}/:id/edit`,  controller.edit)
        this.$rbac.post(`/${name}/:id`,  controller.save)
        this.$rbac.get(`/${name}/:id/destroy`,  controller.destroy)
    }
    
}

module.exports = new Rbac()
